import pymysql
import random
import uuid
import datetime
company=["万御安防"]
scene=["数据中心","办公楼","停车场"]
project=["背景音乐","周界安防","照明系统","活动感知","门禁","监控","上网"]
device_name=["注塑机","挤出机","热压机","冲床","剪切机","折弯机","冷镦机","钻床","磨床","铣床","车床","拉床","锻压机","成型机","压铸机"]
#device_id=[]
#code_label=[]
create_user=[1,2,3]
#=[]
status=[1]
user = [1,2,3]


#create_table()

def in_data_to_sql():
    # 连接数据库
    connect = pymysql.Connect(
        host='172.10.10.6',
        port=3306,
        user='root',
        passwd='Test@123',
        db='matrix',
        charset='utf8'
    )
    calssx = random.choice(device_name)
    # 获取游标
    cursor = connect.cursor()
    sqlstr = 'INSERT INTO serverworker_device_info(`company`,`scene`,`project`,`device_calss`,`device_name`,`device_id`,`admin_user_id`,`create_user_id`,`watch_user_id`,`teach_user_id`,`status`,`create_time`) VALUES("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")'
    cursor.execute(sqlstr%(
        random.choice(company),
        random.choice(scene),
        random.choice(project),
        calssx,
        calssx+"-"+str(random.choice([1,2,3,4,5,6,7,8,9])),
        uuid.uuid4().hex[16:],
        random.choice(user),
        random.choice(user),
        random.choice(user),
        random.choice(user),
        random.choice(status),

        datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    ))
    print(sqlstr)
    connect.commit()  # 提交

    print('成功插入', cursor.rowcount, '条数据')

    # 关闭连接
    cursor.close()
    connect.close()

def update_sql():
    # 连接数据库
    connect = pymysql.Connect(
        host='172.10.10.6',
        port=3306,
        user='root',
        passwd='Test@123',
        db='matrix',
        charset='utf8'
    )

    # 获取游标
    cursor = connect.cursor()
    sqlstr = 'update serverworker_device_info set create_user_id = 2 where status = 1 LIMIT 1'
    cursor.execute(sqlstr%())
    print(sqlstr)
    connect.commit()  # 提交

    print('成功插入', cursor.rowcount, '条数据')

    # 关闭连接
    cursor.close()
    connect.close()

#

if __name__ == '__main__':
    print("mytools ")
    for i in range(1, 20):
        in_data_to_sql()
